package com.itheima.test;

import com.itheima.domain.Student;
import com.itheima.util.GeneratorStudent;

import java.io.*;
import java.time.Period;
import java.util.*;
import java.util.stream.Collectors;

/*
生成40个学生
 */
public class Demo01 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //genratorStudents();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("homework\\list.txt"));
        ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
       //printStudents(list);
        //getNanNv(list);
        //get00hou(list);
        //getNameLength(list);
        //getZhang(list);
        //getTwo6Phones(list);
        //getPhone68s(list);
        //getWXNumberCount(list);
        //getAvgAge(list);
        //get3Oldest(list);
        //get3Youngest(list);
        //getOldestAndYoungest(list);
        //getXingCount(list);
        //getAgeCount(list);
    }

    public static void getAgeCount(ArrayList<Student> list) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (Student student : list) {
            int c = student.getAge();
            if (map.containsKey(c)) {
                Integer count = map.get(c);
                count++;
                map.put(c,count);
            }else {
                map.put(c,1);
            }
        }
        map.forEach((k,v)->{
            System.out.println("年龄是"+k+"的有"+v);
        });
    }

    public static void getXingCount(ArrayList<Student> list) {
        //不同的姓各自有多少学生？
        HashMap<Character, Integer> map = new HashMap<>();
        for (Student student : list) {
            char c = student.getName().charAt(0);
            if (map.containsKey(c)) {
                Integer count = map.get(c);
                count++;
                map.put(c,count);
            }else {
                map.put(c,1);
            }
        }
        map.forEach((k,v)->{
            System.out.println("姓"+k+"的有"+v);
        });
    }

    public static void getOldestAndYoungest(ArrayList<Student> list) {
        TreeSet<Student> treeSet = new TreeSet<>((s1, s2) -> s2.getBirthday().compareTo(s1.getBirthday()));
        for (Student student : list) {
            treeSet.add(student);
        }
        Student first = treeSet.first();
        Student last = treeSet.last();
        System.out.println(first);
        System.out.println(last);
        Period between = Period.between( last.getBirthday(),first.getBirthday());
        System.out.println(between.getYears()+"年"+between.getMonths()+"月"+between.getDays()+"天");
    }

    public static void get3Youngest(ArrayList<Student> list) {
        //最小年龄的三个人
        TreeSet<Student> treeSet = new TreeSet<>((s1, s2) -> s2.getBirthday().compareTo(s1.getBirthday()));
        for (Student student : list) {
            treeSet.add(student);
        }
        ArrayList<Student> list1 = new ArrayList<>(treeSet);
        for (int i = 0; i < 3; i++) {
            System.out.println(list1.get(i));
        }
    }

    public static void get3Oldest(ArrayList<Student> list) {
        //最大年龄的三个人
        TreeSet<Student> treeSet = new TreeSet<>((s1, s2) -> s1.getBirthday().compareTo(s2.getBirthday()));
        for (Student student : list) {
            treeSet.add(student);
        }
        ArrayList<Student> list1 = new ArrayList<>(treeSet);
        for (int i = 0; i < 3; i++) {
            System.out.println(list1.get(i));
        }
    }

    //分析每组的平均年龄
    public static void getAvgAge(ArrayList<Student> list) {
        //创建map
        HashMap<Integer, ArrayList<Student>> map = new HashMap<>();
        //遍历集合  分析人员该在的组
        for (Student student : list) {
            int group = student.getGroup();
            boolean b = map.containsKey(group);
            ArrayList<Student> list1;
            if(b){
                list1= map.get(group);
            }else {
               list1 = new ArrayList<>();
            }
            list1.add(student);
            map.put(group,list1);
        }
        HashMap<Integer, Integer> ageMap = new HashMap<>();
        //一个组一个entry
        Set<Map.Entry<Integer, ArrayList<Student>>> entries = map.entrySet();
        for (Map.Entry<Integer, ArrayList<Student>> entry : entries) {

            Integer group = entry.getKey();

            ArrayList<Student> studnets = entry.getValue();
            int sum=0;
            for (Student studnet : studnets) {
                sum+=studnet.getAge();
            }
            int avg=sum/studnets.size();
            ageMap.put(group,avg);

        }
        //遍历年龄集合
        ageMap.forEach((k,v)-> System.out.println("第"+k+"组的平均年龄是"+v));
    }

    /*
    分析微信号数量
     */
    public static void getWXNumberCount(ArrayList<Student> list) {
        int count1 = 0;
        int count2 = 0;
        for (Student student : list) {
            if (student.getWxNumber().size() == 1) {
                count1++;
            } else if (student.getWxNumber().size() == 2) {
                count2++;
            }
        }
        System.out.println("有1个微信号的" + count1);
        System.out.println("有2个微信号的" + count2);
    }

    public static void getPhone68s(ArrayList<Student> list) {
        List<Student> phone68s = list.stream().filter(s -> {
            int count = 0;
            int i = s.getPhone().indexOf("6");
            if (i >= 0) {
                count++;

            }
            int i1 = s.getPhone().indexOf("8");
            if (i1 >= 0) {
                count++;
            }
            return count == 2;

        }).collect(Collectors.toList());
        for (Student phone68 : phone68s) {
            System.out.println(phone68);
        }
    }

    /*
    分析手机号有两个6

     */
    public static void getTwo6Phones(ArrayList<Student> list) {
        List<Student> two6Phones = list.stream().filter(s -> {
            int count = 0;
            int i = s.getPhone().indexOf("6");
            if (i >= 0) {
                count++;
                int index = s.getPhone().indexOf("6", i + 1);
                if (index >= 0) {
                    count++;
                }
            }
            return count == 2;

        }).collect(Collectors.toList());
        for (Student two6Phone : two6Phones) {
            System.out.println(two6Phone);
        }
    }

    /*
    分析姓张的人数
     */
    public static void getZhang(ArrayList<Student> list) {
        long length = list.stream().filter(s -> s.getName().startsWith("李")).count();
        System.out.println("姓张的有" + length);
    }

    /*
    分析名字长度
     */
    public static void getNameLength(ArrayList<Student> list) {
        long count = list.stream().filter(s -> s.getName().length() == 2).count();
        long count2 = list.stream().filter(s -> s.getName().length() == 3).count();
        System.out.println("名字是两个的" + count);
        System.out.println("名字是三个的" + count2);
    }

    public static void printStudents(ArrayList<Student> list) {
        for (Student student : list) {
            System.out.println(student);
        }
    }

    //分析00 后数量
    public static void get00hou(ArrayList<Student> list) {
        long count = list.stream().filter(s -> s.getBirthday().getYear() >= 2000).count();
        System.out.println(count);
    }

    /*
    分析性别
     */
    public static void getNanNv(ArrayList<Student> list) {
        int nanCount = 0;
        int nvCount = 0;
        for (Student student : list) {
            System.out.println(student);
            if (student != null) {
                if (student.getGender().equals("男")) {
                    nanCount++;
                } else {
                    nvCount++;
                }
            }
        }
        System.out.println("男生有" + nanCount);
        System.out.println("女生有" + nvCount);
    }

    public static void genratorStudents() throws IOException {
        ArrayList<Student> list = getStudents();
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("homework\\list.txt"));
        oos.writeObject(list);
        oos.close();
    }

    public static ArrayList<Student> getStudents() {
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < 40; i++) {
            Student student = GeneratorStudent.getStudent();
            //学号
            String s = "TYJAVA27" + (i <= 8 ? "0" + (i + 1) : (i + 1));
            student.setSid(s);
            int group = i % 10 + 1;
            student.setGroup(group);
            list.add(student);
        }
        return list;
    }
}
